<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        #result{
            width: 400px;
            height: 300px;
            border:solid 1px #033;
        }
    </style>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            // 
            state = {
                msg: []
            }

            render(){
                return <div>
                        <input type="text" onKeyDown={this.keydown} />
                        <hr />
                        <ul id="result" ref={el => this.result = el}> 
                            {
                                this.state.msg.map((item, index) => {
                                    return <li key={index}>{item}</li>
                                })
                            }
                        </ul>
                    </div>
            }

            keydown = (e) => {
                // console.log('你按下了键盘');
                // console.log(e.keyCode);
                //检测当前是否按下了 『回车键』
                if(e.keyCode === 13){
                    //获取 input 元素中的值
                    let v = e.target.value;
                    //设置到 result 中
                    // this.result.innerHTML = v;
                    this.setState({
                        msg: [...this.state.msg, v]
                    })
                    //清空 input 元素的值
                    e.target.value = '';
                }
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>